<?php
namespace app\admin\controller;

use data\model\ArtTag;
use data\model\Tags;
use think\Db;
use think\Loader;
use data\model\Cata;
use data\model\Article as Articles;
/**
 * 文章列表
 * User: Administrator
 * Date: 2017/10/28
 * Time: 16:47
 */
class Article extends Base
{
    /*文章列表*/
    public function ls(){
        $art = new Articles();
        $res = $art->with('catas')->paginate(10);
//        var_dump($res->toArray());die();
        $this->assign('res',$res);
        return $this->fetch('admin/article/ls');
    }

    /*添加文章*/
    public  function add(){
        $catares = Cata::all();
        $this->assign('catares',$catares);
        if(request()->post()) {
            $data = [
                'arttitle' => input('title'),
                'desc' => input('desc'),
                'content' => str_replace(',', ',', input('content')),
                'cataid' => input('cataid'),
                'author' => input('author'),
                'tag' => input('keywords'),
                'time' => time(),
            ];
            if (input('state') == 'on') {
                $data['state'] = 0;
            }
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('pic');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if ($file) {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if ($info) {
                    $data['pic'] = 'uploads/' . $info->getSaveName();
                }
            }
            $art = new Articles();
            $artres = $art->validate('article')->save($data);
            if (false === $artres) {
                $this->error($art->getError());
                exit();
            }
            //设置标签
            $tag = $this->ralate(input('keywords'), $art->artid);
            if ($tag && $art) {
                $this->success('添加文章成功', 'ls');
            } else {
                \data\model\Article::destroy($art->artid);
                $this->error('添加失败请重新添加');
            }
        }
        return $this->fetch('admin/article/add');
    }

    /*编辑文章*/
    public function edit(){
        $id = input('id');
        //栏目
        $catares = Cata::all();
        //文章内容
        $art = Articles::get(['artid'=>$id]);
        $this->assign('catares',$catares);
        $this->assign('art',$art);

        if(request()->post()){
            $data =[
                'arttitle' =>input('title'),
                'desc' =>input('desc'),
                'content' =>str_replace(',',',',input('content')),
                'cataid' =>input('cataid'),
                'author' =>input('author'),
                'tag' =>input('keywords'),
                'time' =>time(),
            ];
            if(input('state')=='on'){
                $data['state'] = 0;
            }
            $file = request()->file('pic');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                $data['pic'] = 'uploads/'.$info->getSaveName();
            }
            $validate = Loader::validate('article');
            if(!$validate->check($data)){
                $this->error($validate->getError());die();
            }
            $tags = $this->ralate(input('keywords'),input('id'),1);
            $art = Db::name('article')->where('artid',input('id'))->update($data);
            if($art&&$tags){
                $this->success('文章修改成功','ls');
            }else{
                $this->error('修改失败请重新修改');
            }
        }
        return $this->fetch('admin/article/edit');
    }

    /*删除文章*/
    public  function  del(){
        $id = input('id');
        $rs = Articles::destroy($id);
        ArtTag::destroy(['artid'=>$id]);
//        Db::name('art_tag')->where('artid',$id)->delete();
        if($rs){
            $this->success('删除成功','ls');
        }else{
            $this->error('删除失败');
        }
    }


    /*将关键词转成数组存入数据库*/
    public function ralate($keywords,$artid,$type = 0){
        $arr=explode(',', $keywords);
        // var_dump($arr); die;
        if($type == 1){
            Db::name('art_tag')->where('artid',$artid)->delete();
        }
        //标签id
        $tids = array();
        //标签内容
        $tc = array();
        /*查询tag标签是否已经在数据库中 */
        foreach ($arr as $k => $v) {
            $rs = Tags::getByTname($v);
            if ($rs) {
                $tids[] = $rs->tid;
            } else {
                $tc[] = $v;
            }
        }
        /*将数据库为存在的标签插入数据库*/
        if(count($tc)>0){
            $rs = array();
            foreach ($tc as $v){
                $rs[] = array('tname'=>$v);
            }
            $tags = new Tags();
            $rs = $tags->saveAll($rs);
            foreach ($rs as $t){
                $tids[] = $t->tid;
            }
        }
        /*将文章id 和标签id 存入文章标签中间表*/
        $rs = array();
        foreach ($tids as $id){
            $rs[]=array('artid'=>$artid,'tid'=>$id);
        }
        $at = new ArtTag();
        $ats =$at->saveAll($rs);
        if($ats){
            return true;
        }else{
            return false;
        }
    }
}